using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using english_sentences.Services.Interface;
using Newtonsoft.Json;

namespace english_sentences.Services
{
    public class TranslatorService : ITranslatorService
    {
        private readonly string EndPoint = "https://api.cognitive.microsofttranslator.com";
        private readonly string location;
        private readonly string key;
        private readonly IConfiguration _configuration;
        public TranslatorService(IConfiguration configuration)
        {
            this._configuration = configuration;
            // Retrieve the connection string for use with the application. 
            this.key = _configuration.GetSection("TRANSLATOR_KEY").Value;
            this.location = _configuration.GetSection("TRANSLATOR_LOCATION").Value;
        }

        public async Task<string> TranslateToChinese(string text)
        {
            string route = "/translate?api-version=3.0&from=en&to=zh";

            return await this.Translate(route, text);

        }
        public async Task<string> TranslateToEnglish(string text)
        {
            string route = "/translate?api-version=3.0&from=zh&to=en";

            return await this.Translate(route, text);

        }

        private async Task<string> Translate(string route, string text)
        {
            object[] body = new object[] { new { Text = text } };
            var requestBody = JsonConvert.SerializeObject(body);

            using (var client = new HttpClient())
            using (var request = new HttpRequestMessage())
            {
                // Build the request.
                request.Method = HttpMethod.Post;
                request.RequestUri = new Uri(this.EndPoint + route);
                request.Content = new StringContent(requestBody, Encoding.UTF8, "application/json");
                request.Headers.Add("Ocp-Apim-Subscription-Key", key);
                // location required if you're using a multi-service or regional (not global) resource.
                request.Headers.Add("Ocp-Apim-Subscription-Region", location);

                // Send the request and get response.
                HttpResponseMessage response = await client.SendAsync(request).ConfigureAwait(false);
                // Read response as a string.
                var result = await response.Content.ReadFromJsonAsync<TranslateResult[]>();//.ReadAsStringAsync();

                if (result == null) return string.Empty;

                var returnVal = result.Length > 0 ? result[0] : null;

                if (returnVal == null || returnVal.translations == null) return string.Empty;

                return returnVal.translations.Length > 0 ? returnVal.translations[0].text ?? "" : "";

            }
        }
    }
    public class TranslateResult
    {
        public Translation[] translations { get; set; }
    }
    public class Translation
    {
        public string text { get; set; }
        public string to { get; set; }
    }

}
